
export default{
	state:{
		cartList:[],
		selectedList:[],
		flag:'flase',
	},
	getters:{
		//判断是否全选
		checkedAll(state){
			return state.cartList.length===state.selectedList.length;
		},
		//合计 
		totalCount(state){
			let total={
				pprice:0,
				num:0
			}
			state.cartList.forEach(v=>{
				if(state.selectedList.indexOf(v.id)>-1){
					total.pprice+=v.pprice*v.num;
					total.num=state.selectedList.length;
				}
			})
			return total;
		}
	},
	mutations:{
		//全选
		checkAll(state){
			state.selectedList=state.cartList.map(v=>{
				v.checked=true;
				return v.id
				
			})
		},
		//全不选
		unCheckAll(state){
			state.cartList.forEach(v=>{
				v.checked=false;
			})
			state.selectedList=[];
		},
		//单选  
		selectedItem(state,index){
			let id =state.cartList[index].id;
			let i=state.selectedList.indexOf(id);
			//看是否在选中列表里,则移除selected
			if(i>-1){
				state.cartList[index].checked=false;
				return state.selectedList.splice(i,1);
				//删除数组中元素，一个
			}
			//如果没有选中，就加入选中列表
			state.cartList[index].checked=true;
			return state.selectedList.push(id);
		}
		
	}, 
	actions:{
		checkedAllFn(context,state){
			console.log(state.flag);
				context.getters.checkedAll ? context.commit("unCheckAll") : context.commit("checkAll");
		}
		
	}
}